package top.jle.jpa.basic.dao;

import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import org.hibernate.query.criteria.HibernateCriteriaBuilder;

import java.util.Arrays;
import java.util.Objects;

public final class AndPreFilter implements PreFilter {

	private final PreFilter[] preFilters;

	AndPreFilter(PreFilter[] preFilters){
		if(preFilters==null || preFilters.length<2){
			throw new IllegalArgumentException("length of preFilters must be greater than or equal 2");
		}
		this.preFilters =preFilters;
	}

	@Override
	public <T> Predicate buildCriterion(Root<T> root, HibernateCriteriaBuilder builder) {
		return builder.and(Arrays.stream(preFilters).filter(Objects::nonNull).map(preFilter -> preFilter.buildCriterion(root,builder)).toArray(Predicate[]::new));
	}

}
